package com.zyz.controller;

import com.zyz.common.ResultInfo;
import com.zyz.domain.User;
import com.zyz.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;

//用户登录
@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private LoginService loginService;


    //用户密码 账号 身份验证
    @PostMapping
    public ResultInfo login(@RequestBody Map<String,String> map, HttpSession request){
        //获取用户账号 密码 身份
        String userName = map.get("userName");
        String password=map.get("password");
        String type = map.get("type");
        User user = new User();
        user.setUsername(userName);
        user.setPassword(password);
        user.setType(Integer.valueOf(type));
        //调用service
        User userInfo=loginService.login(user);
        if (userInfo!= null){
            request.setAttribute("userInfo",userInfo);
        }
        String msg=null;
        if(type.equals("1")){
            msg="1";
        }else if (type.equals("2")){
            msg="2";
        }
//        判断用户类型 如果是会员 返回msg="1"  管理员msg="2"
        return ResultInfo.success(msg);
    }

}
